package mu.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname LeetCode_70
 * @Description 爬楼梯
 *          假设你正在爬楼梯，需要n阶你才能到达楼顶。
 *          每次你可以爬1或者2个台阶。你有多少种不同的方法可以爬到楼顶呢？
 * @Date 2023/1/28 15:14
 * @Created by JiangMuyun
 */
public class LeetCode_70 {
    /*循环解法，自底向上累加*/
    public static int climbStairs(int n){
        if (1==n) return 1;
        if (2==n) return 2;
        int result = 0;
        int pre = 2;
        int prePre = 1;
        for (int i = 3; i <= n; ++i) {
            result = prePre + pre;
            prePre = pre;
            pre = result;
        }
        return result;
    }

    private static Map<Integer,Integer> map = new HashMap<>();
    public static int climbStairsPlus(int n){
        if (n==1) return 1;
        if (n==2) return 2;
        if (null != map.get(n)){
            return map.get(n);
        }
        else {
            int result = climbStairsPlus(n-1)+climbStairsPlus(n-2);
            map.put(n,result);
            return result;
        }
    }

    public static void main(String[] args) {
        //System.out.println(climbStairs(3));
        System.out.println(climbStairsPlus(3));
    }
}
